<main class="post-books-main">
    <header class="post-books-left">
        <div class="book-cover">
            <img data-src="<?php $this->fields->books_cover();?>" alt="<?php $this->title(); ?>" class="post-thumbnail">
    		<div class="dynamic-background"></div>
            <div class="leftinfo">
                <?php
                        $books = [];
                        $books_text = $this->fields->books_content; // 假设 books_content 存储了数据
                        if ($books_text) {
                        $books_arr = explode("\r\n", $books_text);
                        // 每4行组成一本书的信息
                        for ($i = 0; $i < count($books_arr); $i += 4) {
                            // 确保有足够的数据行
                            if (isset($books_arr[$i]) && isset($books_arr[$i+1]) && isset($books_arr[$i+2]) && isset($books_arr[$i+3])) {
                                $author = trim($books_arr[$i]);      // 第一行: 作者笔名
                                $page_count = trim($books_arr[$i+1]); // 第二行: 全文数字(页数)
                                $publish_date = trim($books_arr[$i+2]);// 第三行: 出版日期
                                $rating = trim($books_arr[$i+3]);     // 第四行: 评分
                                
                                $books[] = array(
                                    "author" => $author,
                                    "page_count" => $page_count,
                                    "publish_date" => $publish_date,
                                    "rating" => $rating
                                );
                            }
                        }
                    }
                ?>
                <?php if (!empty($books)): ?>
                <?php foreach ($books as $book): ?>
                <div class="book-author">
                    <span><?php echo $book['author']; ?></span>
                </div>
                <div>
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M6 3.75a.75.75 0 000 1.5h12a.75.75 0 000-1.5H6zM3 8.25a.75.75 0 01.75-.75h16.5a.75.75 0 010 1.5H3.75a.75.75 0 01-.75-.75zM3 13.5a.75.75 0 01.75-.75h16.5a.75.75 0 010 1.5H3.75a.75.75 0 01-.75-.75zM4.5 18.75a.75.75 0 000 1.5h15a.75.75 0 000-1.5H4.5z"/></svg>
                    <span><?php echo $book['page_count']; ?></span>
                </div>
                <div>
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2.25c-5.385 0-9.75 4.365-9.75 9.75s4.365 9.75 9.75 9.75 9.75-4.365 9.75-9.75S17.385 2.25 12 2.25zm0 1.5c4.135 0 7.5 3.365 7.5 7.5s-3.365 7.5-7.5 7.5-7.5-3.365-7.5-7.5 3.365-7.5 7.5-7.5zm3.75 10.5a.75.75 0 00-.75.75v3.75a.75.75 0 01-1.5 0v-3.75a.75.75 0 00-.75-.75h-3.75a.75.75 0 00-.75.75v3.75a.75.75 0 01-1.5 0v-3.75a.75.75 0 00-.75-.75h-3.75a.75.75 0 00-.75.75v3.75c0 .414.336.75.75.75h15a.75.75 0 00.75-.75v-3.75a.75.75 0 00-.75-.75h-3.75z"/></svg>
                    <span>积分：<?php echo $book['rating']; ?></span>
                </div>
                <div>
                    <span class="copyright-status"><?php echo $book['publish_date']; ?></span>
                </div>
                <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </header>
    <article class="main-content">
        <div class="text-layout">
            <?php _article_changetext($this); ?>
        </div>
<?php
 $GLOBALS['isLogin'] = $this->user->hasLogin();
 $GLOBALS['rememberEmail'] = $this->remember('mail',true);
function threadedComments($comments, $options)
{
    $commentClass = '';
    if ($comments->authorId) {
        if ($comments->authorId == $comments->ownerId) {
            $commentClass .= ' comment-by-author';
        } else {
            $commentClass .= ' comment-by-user';
        }
    }
    $commentLevelClass = $comments->levels > 0 ? ' comment-child' : ' comment-parent';
    if ($comments->url) {
        $author = '<a href="' . $comments->url . '"' . '" target="_blank"' . ' rel="external nofollow">' . $comments->author . '</a>';
    } else {
        $author = $comments->author;
    }
    ?>
    <li id="li-<?php $comments->theId(); ?>" class="comment-body<?php
                                                                if ($comments->levels > 0) {
                                                                    echo ' comment-child';
                                                                    $comments->levelsAlt(' comment-level-odd', ' comment-level-even');
                                                                } else {
                                                                    echo ' comment-parent';
                                                                }
                                                                $comments->alt(' comment-odd', ' comment-even');
                                                                echo $commentClass;
                                                                ?>">
        <div id="<?php $comments->theId(); ?>">
            
            <div class="comment-content">
                <div class="comment-text">
                    <?php $comments->content();?>
                </div>
                
            </div>
            

        </div>
        <?php if ($comments->children) { ?><div class="comment-children"><?php $comments->threadedComments($options); ?></div><?php } ?>
    </li>
<?php } ?>

<div id="comments" class="comments-main">
    <?php $this->comments()->to($comments); ?>
    
    <div class="books_lie">
    <?php if ($comments->have()) : ?>
        <?php $comments->listComments(); ?>
        <div class="paging">
            <?php $comments->pageNav('', ''); ?>
            <!-- 修改评论样式 -->
            <?php
                // 获取当前页面的文章 CID
                $cid = $this->cid;
                // 查询当前页面的评论总数
                // 查询当前页面的评论总数，只查询 parent 为 0 的评论
                $comment_count = $this->db->fetchRow($this->db->select('COUNT(*) AS count')->from('table.comments')
                    ->where('cid = ?', $cid)
                    ->where('status = ?', 'approved')
                    ->where('parent = ?', 0))['count'];//只查询父级评论，否则会导致分页存在问题
                    
                if($comment_count > 6)://注意 这里的数字一定要与主题设置中分页显示的评论数一致，换言之 如果评论数有下一页 则显示加载更多按钮?>
                
                <!-- 添加一个按钮用来加载下一页评论 -->
                <div id="loading-spinner">
                    <div class="spinner"></div>
                    <span>加载中...</span>
                </div>
                <?php endif; ?>
                <!-- 添加一个用于显示提示信息的元素 -->
                <div class="end">
                    <div id="no-more" style="display: none;">END</div>
                </div>
            <?php endif; ?>
        </div>
    </div>
    <?php if ($this->allow('comment')) : ?>
        <div id="<?php $this->respondId(); ?>" class="respond">
            <form method="post" action="<?php $this->commentUrl() ?>" id="comment-form" role="form" class="comment-form">
    <div class="books-container">
        <div class="comment-main">
            <?php if ($this->user->hasLogin()) : ?>
            <div class="comment-fields">
                
                    <div class="admin-info">尊敬的站长<a class="admin-link" href="<?php $this->options->profileUrl(); ?>"><?php $this->user->screenName(); ?></a>, 欢迎回来! <a href="<?php $this->options->logoutUrl(); ?>" title="Logout"><?php _e('退出'); ?> &raquo;</a></div>
            </div>

            <div class="comment-editor">
                <textarea name="text" id="textarea" placeholder="撰写笔记..." class="comment-textarea" required onkeydown="if((event.ctrlKey||event.metaKey)&&event.keyCode==13){document.getElementById('submitComment').click();return false};"><?php $this->remember('text'); ?></textarea>
                
                <div class="comment-footer">
                        <button id="submitComment" type="submit" class="submit-btn">发表</button>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>
</form>
        </div>
    <?php else : ?>
            <div class="comments_off">
                <svg xmlns="http://www.w3.org/2000/svg" width="1.5em" height="1.5em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-lock-keyhole-icon lucide-lock-keyhole"><circle cx="12" cy="16" r="1"/><rect x="3" y="10" width="18" height="12" rx="2"/><path d="M7 10V7a5 5 0 0 1 10 0v3"/></svg>
                <?php _e('已关闭笔记功能'); ?>
            </div>
    <?php endif; ?>
</div>


    </article>
</main>